﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Navigation 的摘要说明
/// </summary>
public partial class Nava
{
    public enum type { 辅导 = 1, 培训 = 2 }

    public static Nava Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Nava.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<Nava> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Nava.ToList();
        }
    }
    public static string GetNavaTitleByIDs_Type(object ID,int TypeID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            string Sql = String.Format("select Title+',' from dbo.Nava where CONVERT(varchar,ID) in ({0}) and Type={1} For XML PATH('') ", ID.ToString(), TypeID);
            return context.ExecuteQuery<string>(@Sql).FirstOrDefault();
        }
    }

    public static Nava GetByTitle(string Title)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Nava.FirstOrDefault(row => row.Title.Contains(Title) || Title.IndexOf(row.Title) > -1);
        }
    }
    public static List<Nava> GetAllByParentID(int ParentID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Nava.Where(row => row.ParentID == ParentID).ToList();
        }
    }
    public static List<Nava> GetAllErJiByParentID_TypeID(int ParentID, int TypeID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from n in context.Nava
                        where n.ParentID == n.RootID && n.RootID != 0 && n.Type == TypeID
                        select n;
            if (ParentID != 0)
                query = query.Where(row => row.ParentID == ParentID);
            return query.ToList();
        }
    }


    public static List<Nava> GetAllByParentID_TypeID(int ParentID, int TypeID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from n in context.Nava
                        where n.ParentID == ParentID && n.Type == TypeID
                        select n;

            return query.ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Nava.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Nava>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            //删除导航ID=id的导航以及父导航为id的导航
            context.ExecuteCommand(@"delete from  dbo.Nava  where ID={0} or ParentID={0} or RootID={0} ", id);
        }
    }

}